<?php
if (isset($_SESSION['player_id'])) {
	
	
	
$query = "SELECT `role_id`, `team_id`
				FROM `players`
				WHERE `id`='{$_SESSION['player_id']}'
				LIMIT 1";
	$sql = mysql_query($query) or die(mysql_error());
	if (mysql_num_rows($sql)==1)
	{
	   $cap=mysql_fetch_assoc($sql);
       if ($cap['role_id']!=1 && $cap['team_id']==''){

if (isset($_GET['action']) && $_GET['action'] == 'invite' && isset( $_GET['id']) && is_numeric($_GET['id'])) {
	$query = "INSERT INTO `invite`
					SET
						`team_id`={$_GET['id']},
						`player_id`={$_SESSION['player_id']}";
	mysql_query($query) or die(mysql_error());
	print 'Заявка на вступление в команду подана.<br />
		<form action="index.php?section=team&mode=invite" method="post">
		
		<input type="hidden" name="revolt" value="revolt" />
		<input type="submit" name="press" value="Отозвать" />
		</form>';	
	}
else {


        $sqlresult = mysql_query("SELECT  *
		FROM invite WHERE `player_id`= {$_SESSION['player_id']}");
        $numrows = mysql_num_rows($sqlresult);
		if ($numrows > 0) {
		if 	(isset($_POST['revolt'])){
        mysql_query("DELETE FROM invite WHERE `player_id`= {$_SESSION['player_id']}");
		print 'Заявка отозвана, <a href="index.php?section=team">подать другую</a>';
		
		}
		else
		{print 'Вы уже подали заявку на вступление в команду.
		<form action="index.php?section=team&mode=invite" method="post">
		
		<input type="hidden" name="revolt" value="revolt" />
		<input type="submit" name="press" value="Отозвать" />
		</form>

		';}
		
		}
		else{ 
			
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 <tr bgcolor="#000000"> 
  <td width="100%">
  <table border="0" width="100%">
    <tr bgcolor="#222222"> 
	<td valign="bottom" colspan="2" > 
	№
	</td>
	<td valign="bottom" colspan="2" > 
	Название
	</td>
	<td valign="bottom" colspan="2" > 
	Капитан
	</td>
	<td valign="bottom" colspan="2" > 
	Заявка
	</td>
	</tr>
<?php
        $sqlresult = mysql_query("SELECT  `login`, `team`, teams.id
         FROM players, teams WHERE (`cap_id`= players.id) ORDER BY teams.id");
        $numrows = mysql_num_rows($sqlresult);
        for ($i=1;$i<=$numrows;$i++){
        $sqlteam = mysql_fetch_assoc($sqlresult);
        echo "<tr bgcolor=\"#333333\"> 
            <td valign=\"bottom\" colspan=\"2\" >{$i}</td>
            <td valign=\"bottom\" colspan=\"2\" >{$sqlteam['team']}</td>
            <td valign=\"bottom\" colspan=\"2\" >{$sqlteam['login']}</td>
			<td valign=\"bottom\" colspan=\"2\" >
	

			<a href=\"index.php?section=team&mode=main&action=invite&id={$sqlteam['id']}\">[Подать]</a></td></tr>";
}

        print '</table>	</td>	</tr>	</table>';

	
	
}	
}	
}
}
}
else print ('Время вашей сессии истекло!');

?>